<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Controller;
use App\Models\News;
use Illuminate\Http\Request;

class NewsController extends BaseController // Controller?
{
    //
    public function index() {
        $this->response();
    }


    public function lists(Request $request) {
        $page = $request->input('page', 1);
        $pageSize = 10;
        $offset = ($page - 1) * $pageSize;
        $data = News::select('id', 'title', 'image', 'publish_from', 'publish_date')
            ->offset($offset)->limit($pageSize)->get();

        foreach ($data as $key => $val){
            $data[$key]['image'] = url('uploads/' . $val['image']);
        }

        $is_last = 0;
        if (count($data) < $pageSize) $is_last = 1;

        $result = [
            'item' => $data,
            'is_last' => $is_last
        ];

        return $this->response(200, '获取数据成功', $result);
    }
}
